package business

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/business"
	businessReq "github.com/flipped-aurora/gin-vue-admin/server/model/business/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type OptionApi struct {
}

var optionService = service.ServiceGroupApp.BusinessServiceGroup.OptionService

// CreateOption 创建选项内容
// @Tags Option
// @Summary 创建选项内容
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body business.Option true "创建选项内容"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /option/createOption [post]
func (optionApi *OptionApi) CreateOption(c *gin.Context) {
	var option business.Option
	err := c.ShouldBindJSON(&option)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = optionService.CreateOption(&option)
	if err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
		return
	}

	response.OkWithMessage("创建成功", c)

}

// DeleteOption 删除选项内容
// @Tags Option
// @Summary 删除选项内容
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body business.Option true "删除选项内容"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /option/deleteOption [delete]
func (optionApi *OptionApi) DeleteOption(c *gin.Context) {
	var option business.Option
	err := c.ShouldBindJSON(&option)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := optionService.DeleteOption(option); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteOptionByIds 批量删除选项内容
// @Tags Option
// @Summary 批量删除选项内容
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除选项内容"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /option/deleteOptionByIds [delete]
func (optionApi *OptionApi) DeleteOptionByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := optionService.DeleteOptionByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateOption 更新选项内容
// @Tags Option
// @Summary 更新选项内容
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body business.Option true "更新选项内容"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /option/updateOption [put]
func (optionApi *OptionApi) UpdateOption(c *gin.Context) {
	var option business.Option
	err := c.ShouldBindJSON(&option)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := optionService.UpdateOption(option); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindOption 用id查询选项内容
// @Tags Option
// @Summary 用id查询选项内容
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query business.Option true "用id查询选项内容"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /option/findOption [get]
func (optionApi *OptionApi) FindOption(c *gin.Context) {
	var option business.Option
	err := c.ShouldBindQuery(&option)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if reoption, err := optionService.GetOption(option.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"reoption": reoption}, c)
	}
}

// GetOptionList 分页获取选项内容列表
// @Tags Option
// @Summary 分页获取选项内容列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query businessReq.OptionSearch true "分页获取选项内容列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /option/getOptionList [get]
func (optionApi *OptionApi) GetOptionList(c *gin.Context) {
	var pageInfo businessReq.OptionSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := optionService.GetOptionInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}
